package org.codeanalyzer.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ApiPackage extends ApiItem
{
    /**
     * Classes contained in the package.
     */
    private Map<String, ApiClass> classes = new HashMap<String, ApiClass>();;

    ApiPackage(String name)
    {
        super(name);
    }

    public Map<String, ApiClass> getClassesMap()
    {
        return Collections.unmodifiableMap(classes);
    }

    public List<ApiClass> getClasses()
    {
        List<ApiClass> classList = new ArrayList<ApiClass>(classes.values());
        Collections.sort(classList);
        return classList;
    }

    public ApiClass addClass(String className)
    {
        ApiClass apiClass;
        String classKey = ApiClass.generateKey(className);
        if (classes.containsKey(classKey))
            apiClass = classes.get(classKey);
        else
            classes.put(classKey, apiClass = new ApiClass(this, className));

        return apiClass;
    }

    /**
     * Returns the total number of classes in the package
     */
    public int getTotalClasses()
    {
        return classes.size();
    }

    public int getTotalMethods()
    {
        int totalMethods = 0;
        for(ApiClass apiClass : classes.values())
            totalMethods += apiClass.getTotalMethods();
        return totalMethods;
    }

    public int getTotalCoveredMethods()
    {
        int coveredMethods = 0;
        for(ApiClass apiClass : classes.values())
            coveredMethods += apiClass.getTotalCoveredMethods();
        return coveredMethods;
    }

    public int getTotalCoveredClasses()
    {
        int totalCoveredClasses = 0;
        for(ApiClass apiClass : classes.values())
            totalCoveredClasses += apiClass.isCovered() ? 1 : 0;
        return totalCoveredClasses;
    }

    public boolean isCovered()
    {
        return getTotalCoveredClasses() > 0;
    }

    public int getClassCoverage()
    {
        return (int)((double)getTotalCoveredClasses() / (double)getTotalClasses() * 100.0);
    }

    public int getMethodCoverage()
    {
        return (int)((double)getTotalCoveredMethods() / (double)getTotalMethods() * 100.0);
    }
}
